#!/user/bin/python
# -*- coding: utf-8 -*-
'''
 @Time    : 2018/5/23 20:20
 @File    : context_entity.py
 @desc    :
'''
from commonSpiders.utils.singleton import singleton


@singleton
class SocketIoContext(object):

    SOCKETIO_KEY = 'socketio'

    def __init__(self):
        self.context = {}

    def set_socketio(self, mac, manager_guid, socketio):
        self.context.setdefault(mac, {})
        self.context[mac].setdefault(manager_guid, {})
        self.context[mac][manager_guid].update({
            self.SOCKETIO_KEY: socketio
        })

    def get_socketio(self, mac, manager_guid):
        temp = self.context.get(mac, None)
        if not temp:
            return None
        temp = temp.get(manager_guid, None)
        if not temp:
            return None
        temp = temp.get(self.SOCKETIO_KEY, None)
        return temp

    def get_socketio(self, mac):
        result = {}
        temp = self.context.get(mac, result)
        for key, item in temp.items():
            result.update(key, item[self.SOCKETIO_KEY])
        return result

    def get_manager_dict(self):
        result = []
        for managers_dict in self.context.values():
            if not managers_dict:
                continue
            for key, manager_dict in managers_dict.items():
                result.append(key)
                result.append(manager_dict[self.SOCKETIO_KEY])
            if result:
                break
        return result
